class Order < ActiveRecord::Base
  has_many :order_items
  has_many :order_commentss
  has_many :artworks, :through => :order_items
  
  def total
    order_items.inject(0) {|sum, n| n.price * n.amount + sum}
  end
  
  def self.full_list(page, status)
    if status == 'all'
      paginate(:per_page => 10, :page => page, :order => 'orders.updated_at')
    else
      paginate(:per_page => 10, :page => page, :order => 'orders.updated_at', :conditions => [ 'status = ?', status])
    end
  end
  
  validates_size_of :order_items, :minimum => 1
  validates_length_of :first_name, :in => 1..255
  validates_length_of :last_name, :in => 1..255
  validates_length_of :address1, :in => 2..255
  validates_length_of :city, :in => 2..255
  validates_length_of :state, :in => 2..255
  validates_length_of :zip, :in => 2..255
  validates_length_of :ship_to_country, :in => 2..255
  
  validates_length_of :phone_number, :in => 7..20
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
#  validates_inclusion_of :status, :in => %w(open processed closed failed)

end
